#include "baseeditor.h"
#include <QFont>
#include <QFontMetrics>
#include <QDebug>

BaseEditor::BaseEditor(QWidget *parent)
    : QsciScintilla(parent)
    , m_lexer(nullptr)
    , m_apis(nullptr)
    , m_syntaxHighlightingEnabled(true)
    , m_autoCompletionEnabled(true)
    , m_autoCompletionThreshold(3)
{
    setupCommonSettings();

    // 连接信号
    connect(this, &QsciScintilla::textChanged, this, &BaseEditor::onTextChanged);
    connect(this, &QsciScintilla::cursorPositionChanged, this, &BaseEditor::onCursorPositionChanged);
}

BaseEditor::~BaseEditor()
{
    if (m_apis) {
        delete m_apis;
        m_apis = nullptr;
    }

    if (m_lexer) {
        delete m_lexer;
        m_lexer = nullptr;
    }
}

QString BaseEditor::getAllText() const
{
    return text();
}

void BaseEditor::setAllText(const QString &text)
{
    setText(text);
}

void BaseEditor::enableSyntaxHighlighting(bool enable)
{
    m_syntaxHighlightingEnabled = enable;
    if (enable && m_lexer) {
        setLexer(m_lexer);
    } else {
        setLexer(nullptr);
    }
}

void BaseEditor::enableCodeFolding(bool enable)
{
    if (enable) {
        setFolding(QsciScintilla::BoxedTreeFoldStyle);
    } else {
        setFolding(QsciScintilla::NoFoldStyle);
    }
}

void BaseEditor::enableAutoIndentation(bool enable)
{
    setAutoIndent(enable);
}

void BaseEditor::enableBraceMatching(bool enable)
{
    if (enable) {
        setBraceMatching(QsciScintilla::SloppyBraceMatch);
    } else {
        setBraceMatching(QsciScintilla::NoBraceMatch);
    }
}

void BaseEditor::enableLineNumbers(bool enable)
{
    if (enable) {
        setMarginType(0, QsciScintilla::NumberMargin);
        setMarginWidth(0, "0000");
        setMarginLineNumbers(0, true);
        setMarginsBackgroundColor(QColor("#f0f0f0"));
        setMarginsForegroundColor(QColor("#666666"));
    } else {
        setMarginType(0, QsciScintilla::SymbolMargin);
        setMarginWidth(0, 0);
    }
}

void BaseEditor::enableCurrentLineHighlighting(bool enable)
{
    setCaretLineVisible(enable);
    if (enable) {
        setCaretLineBackgroundColor(QColor("#ffffcc"));
    }
}

void BaseEditor::enableAutoCompletion(bool enable)
{
    m_autoCompletionEnabled = enable;
    if (enable) {
        setAutoCompletionSource(QsciScintilla::AcsAll);
        setAutoCompletionThreshold(m_autoCompletionThreshold);
        setAutoCompletionCaseSensitivity(false);
        setAutoCompletionUseSingle(QsciScintilla::AcusNever);
    } else {
        setAutoCompletionSource(QsciScintilla::AcsNone);
    }
}

void BaseEditor::setAutoCompletionThreshold(int threshold)
{
    m_autoCompletionThreshold = threshold;
    if (m_autoCompletionEnabled) {
        QsciScintilla::setAutoCompletionThreshold(threshold);
    }
}

void BaseEditor::checkSyntax()
{
    // 基类提供默认实现，子类可以重写
    clearSyntaxErrors();
}

void BaseEditor::clearSyntaxErrors()
{
    clearAnnotations();
}

int BaseEditor::getCurrentLine() const
{
    int line, index;
    getCursorPosition(&line, &index);
    return line;
}

int BaseEditor::getCurrentColumn() const
{
    int line, index;
    getCursorPosition(&line, &index);
    return index;
}

QString BaseEditor::getCurrentLineText() const
{
    return text(getCurrentLine());
}

void BaseEditor::setupCommonSettings()
{
    setupIndentation(); // 配置缩进
    setupMargins();     // 配置边距
    setupColors();      // 配置选色方案

    // 设置字体
    QFont font("Consolas", 10);
    font.setFixedPitch(true);
    setFont(font);

    // 默认启用的功能
    enableLineNumbers(true);
    enableCodeFolding(true);
    enableAutoIndentation(true);
    enableBraceMatching(true);
    enableCurrentLineHighlighting(true);
    enableAutoCompletion(true);
}

void BaseEditor::setupIndentation()
{
    setIndentationsUseTabs(false);
    setIndentationWidth(4);
    setTabWidth(4);
    setAutoIndent(true);
    setIndentationGuides(true);
    setIndentationGuidesBackgroundColor(QColor("#f0f0f0"));
    setIndentationGuidesForegroundColor(QColor("#d0d0d0"));
}

void BaseEditor::setupMargins()
{
    // 行号边距
    setMarginType(0, QsciScintilla::NumberMargin);
    setMarginWidth(0, "0000");
    setMarginLineNumbers(0, true);
    setMarginsBackgroundColor(QColor("#f0f0f0"));
    setMarginsForegroundColor(QColor("#666666"));

    // 折叠边距
    setMarginType(1, QsciScintilla::SymbolMargin);
    setMarginWidth(1, 20);
    setMarginSensitivity(1, true);
}

void BaseEditor::setupColors()
{
    // 设置选择颜色
    setSelectionBackgroundColor(QColor("#316AC5"));
    setSelectionForegroundColor(QColor("#ffffff"));

    // 设置当前行颜色
    setCaretLineBackgroundColor(QColor("#ffffcc"));

    // 设置匹配括号颜色
    setMatchedBraceBackgroundColor(QColor("#ffff00"));
    setUnmatchedBraceBackgroundColor(QColor("#ff0000"));
}

void BaseEditor::onTextChanged()
{
    emit textContentChanged();
}

void BaseEditor::onCursorPositionChanged(int line, int index)
{
    emit cursorPositionChanged(line, index);
}
